<template>
  <div class="brand-container">
    <search-form
      :config-list="searchFormConfig"
      @search="search"
      @reset="reset"
      ref="searchFormRef"
    >
      <template #leftSlot>
        <!--左侧的插槽-->
        <el-button
          icon="Search"
          type="primary"
          @click="handleAdd"
          v-permissions="['brandBindAdd']"
        >
          新增
        </el-button>
      </template>
    </search-form>

    <CustomTable
      :pagination="pagination"
      :table-config="tableConfig"
      @pagination-change="paginationChange"
      @actions-click="actionsClick"
    />
    <setBrandArea ref="setBrandAreaRef" @fetch-data="search" />
  </div>
</template>

<script>
  export default {
    name: 'brand',
  }
</script>

<script setup>
  import CustomTable from '@/views/component/CustomTable'
  import SearchForm from '@/views/component/SearchForm' //引入搜索组件
  import setBrandArea from './components/setBrandArea.vue'
  import { message } from '@/utils/helper'
  import {
    getSupplierBrandList as getList,
    deleteSupplierBrand,
  } from '@/api/brand'
  // 搜索参数
  const queryForm = ref({})
  const setBrandAreaRef = ref(null)
  const tableConfig = reactive({
    columns: [
      {
        label: 'ID',
        prop: 'id',
        sortable: false,
        width: 120,
        fixed: 'left',
      },
      {
        label: '品牌名称',
        prop: 'brand.name',
        sortable: false,
        width: 220,
      },
      {
        label: '经营区域',
        prop: 'area_detail',
        sortable: false,
        width: 320,
      },
      {
        label: '审批状态',
        prop: 'audit',
        sortable: false,
        width: 120,
        customType: 'ElTag',
        tagKeyValue: 'statusTag',
      },
      {
        label: '公司名称',
        prop: 'brand.company_name',
        sortable: false,
        width: 200,
        showOverflowTooltip: true,
      },
      {
        label: '创建时间',
        prop: 'create_time',
        sortable: false,
        width: 220,
      },
      {
        label: '操作',
        action: true,
        fixed: 'right',
        actions: [
          {
            fnName: 'edit',
            title: '编辑',
            type: 'primary',
            isTextBtn: true,
            permissionCode: 'brandBindEdit', // 权限
            key: 'audit',
            showValue: 0,
            visibleFc: (row) => {
              return [0, 2].includes(row.audit)
            },
          },
          {
            fnName: 'delete',
            title: '删除',
            type: 'primary',
            isTextBtn: true,
            permissionCode: 'brandBindDel', // 权限
          },
        ],
      },
    ],
    tagConfig: {
      statusTag: {
        0: {
          name: '待审核',
          type: 'warning',
        },
        1: {
          name: '审批通过',
          type: 'success',
        },
        2: {
          name: '审核不通过',
          type: 'danger',
        },
      },
    },
    list: [],
    listLoading: false,
  })

  //搜索组件配置信息
  const searchFormConfig = reactive({
    config: {
      layoutType: 1, //1:左右布局,2:上下布局
      leftPanelWidth: 0, //控制左边布局控件宽度
    },
    controlList: [
      {
        label: '审批状态',
        prop: 'audit',
        type: 'select',
        list: Object.keys(tableConfig['tagConfig']['statusTag']).reduce(
          (acc, cur) => {
            const { name } = tableConfig['tagConfig']['statusTag'][cur]
            acc.push({ label: name, value: cur })
            return acc
          },
          []
        ),
      },
    ],
  })

  //点击搜索触发
  const search = (form) => {
    console.log(form)
    pagination.page = 1
    queryForm.value = form
    queryProject()
  }

  //列表分页参数
  const pagination = reactive({
    page: 1, //每页条数
    limit: 10, //当前页
    total: 0,
    layout: 'total, sizes, prev, pager, next, jumper',
  })

  //分页控件点击操作
  const paginationChange = (data) => {
    const { val, type } = data
    pagination[type] = val
    queryProject()
  }

  //请求列表数据
  const queryProject = () => {
    tableConfig.listLoading = true
    getList({
      ...queryForm.value,
      limit: pagination.limit,
      page: pagination.page,
    })
      .then((result) => {
        console.log('result', result)
        tableConfig.list = result.data.data
        tableConfig.listLoading = false
        pagination.total = result.data.total
      })
      .catch(() => {
        tableConfig.listLoading = false
      })
  }
  const handleDelete = (item) => {
    ElMessageBox.confirm(`确定要删除该条记录吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(async () => {
      await deleteSupplierBrand({
        id: item.id,
      })
      message('删除成功')
      queryProject()
    })
  }
  //点击表格操作按钮回调
  const actionsClick = (data) => {
    console.log('actionsClick===', data)
    const { type, row } = data
    switch (type) {
      case 'edit':
        handleEdit(row)
        break
      case 'delete':
        handleDelete(row)
        break
    }
  }
  const handleAdd = () => {
    setBrandAreaRef.value.open()
  }
  const handleEdit = (item) => {
    setBrandAreaRef.value.open(item)
  }
  const searchFormRef = ref(null)
  const reset = () => {
    if (!searchFormRef.value) return
    searchFormRef.value.resetFormFields()
    queryProject()
  }

  const init = async () => {
    await queryProject()
  }

  init()
</script>

<style lang="scss" scoped></style>
